﻿using MediatR;
using U.B.DoMain;
using U.B.ErrorCode;
using U.B.ReadApi.Command;
using U.B.ReadApi.Dto;
using U.B.Repistory;

namespace U.B.ReadApi.CommandHander
{
    public class ReverseRoleMenuCommandHander : IRequestHandler<ReverseRoleMenuCommand, ApiResult<List<RMDto>>>
    {
        public readonly IRepistory<Role> _roleRepository;
        public readonly IRepistory<Menu> _menuRepository;
        public readonly IRepistory<RoleMenu>   _roleMenuRepository;

        public ReverseRoleMenuCommandHander(IRepistory<Role> roleRepository, IRepistory<Menu> menuRepository, IRepistory<RoleMenu> roleMenuRepository)
        {
            _roleRepository = roleRepository;
            _menuRepository = menuRepository;
            _roleMenuRepository = roleMenuRepository;
        }
        /// <summary>
        /// 角色树状点击事件
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<ApiResult<List<RMDto>>> Handle(ReverseRoleMenuCommand request, CancellationToken cancellationToken)
        {
           ApiResult<List<RMDto>> apiResult= new ApiResult<List<RMDto>>();
            var RM = from A in _roleRepository.GetAll()
                     join B in _roleMenuRepository.GetAll() on A.RoleId equals B.RoleId
                     join C in _menuRepository.GetAll() on B.MenuId equals C.MenuId
                     where A.RoleId == request.RoleId
                     select new RMDto
                     {
                         RoleId = A.RoleId,
                         RoleName = A.RoleName,
                         MenuId = B.MenuId,
                         MenuName = C.MenuName,
                         RoleMenuId = B.RoleMenuId
                     };
                     apiResult.Data= RM.ToList();
            apiResult.code = Renum.显示成功;
            apiResult.Message=Renum.显示成功.ToString();
            return Task.FromResult(apiResult);
                  
        }
    }
}
